/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.sys.service.impl.SysDictServiceImpl.java 
 * <p>
 * 类产生时间: 2014/5/26 0026 下午 19:29
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.sys.entity.DictItem;
import com.oceansoft.mobile.econsole.modules.sys.persistence.ISysDictMapper;
import com.oceansoft.mobile.econsole.modules.sys.service.ISysDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 系统字典业务组件实现类
 *
 * @author: chenw
 * @time: 2014/5/26 0026 下午 19:29
 */
@Service
public class SysDictServiceImpl implements ISysDictService {
    @Resource
    private ISysDictMapper sysDictMapper;

    @Override
    public List<DictItem> findByParentId(int parentId) {
        return sysDictMapper.findByParentId(parentId);
    }

    @Override
    public List<DictItem> findByAutoType(int autoType) {
        return sysDictMapper.findByAutoType(autoType);
    }

    @Override
    public Pager<DictItem> findPageByCondition(Query query, int pid) {
        HashMap m = new HashMap();
        m.put("keywords", query.getKeywords());
        m.put("orderField", query.getOrderField());
        m.put("orderDirection", query.getOrderDirection());
        m.put("startRow", query.getStartRow());
        m.put("endRow", query.getEndRow());
        m.put("pid", pid);
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<DictItem> page = (Page<DictItem>) sysDictMapper.findPageByCondition(m);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Result save(DictItem dictItem) {
        int id = sysDictMapper.save(dictItem);
        if (0 < id) {
            dictItem.setId(id);
            return new Result(Result.SUCC, dictItem);
        } else {
            return new Result(Result.FAILURE, "字典新增失败！");
        }
    }

    @Override
    public Result update(DictItem dictItem) {
        int id = sysDictMapper.update(dictItem);
        if (0 < id) {
            return new Result(Result.SUCC, dictItem);
        } else {
            return new Result(Result.FAILURE, "字典修改失败！");
        }
    }

    @Override
    public DictItem findById(int dictId) {
        return sysDictMapper.findById(dictId);
    }

    @Override
    public Result deleteDictItemById(int dictId) {
        int id = sysDictMapper.deleteDictById(dictId);
        if (0 < id) {
            return new Result(Result.SUCC);
        } else {
            return new Result(Result.FAILURE, "字典删除失败！");
        }
    }

    @Override
    public String findAllDictByZtree() {
        //瓶装JSON数据格式
        StringBuilder ztree = new StringBuilder("[");
        List<DictItem> dictItems = sysDictMapper.findAllDictByZtree();
        int _len = dictItems.size();
        for (int i = 0; i < _len; i++) {
            DictItem d = dictItems.get(i);
            //如果是根节点就需要PID并默认打开
            if (d.getId() == 0)
                ztree.append(String.format("{\"id\":0,\"name\":\"%s\",\"open\":true}", d.getTitle()));
                //如果有子节点才加入树 不显示没有子节点的节点
            else {
                ztree.append(String.format("{\"id\":%d,\"pId\":%d,\"name\":\"%s\"}", d.getId(), d.getParentId(), d.getTitle()));
                if (i < _len - 1)
                    ztree.append(",");
            }
        }
        ztree.append("]");
        System.out.println("\n\n" + ztree.toString());
        return ztree.toString();
    }
}
